/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.composite.figure;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.uml2.uml.Element;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.figures.NodeFigure;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;

/**
 * Figure for a collaboration use.
 */
public class CollaborationUseFigure extends NodeFigure {
	
	/** debug channel */
	protected final static int debugChannel=6;
	
	/**
	 * 
	 */
	int [] dashes= new int[10];
	
	/**
	 * 
	 */
	protected boolean extern= false;
	
	/**
	 * 
	 */
	public static Color classColor = PapyrusPlugin.colorManager.get(new RGB(227,221,239));
	
	/**
	 * 
	 */
	public static Color classBorderColor= PapyrusPlugin.colorManager.get(new RGB(166,157,183));

	/**
	 * 
	 */
	protected UMLClassHeaderFigure headerFigure; //head of the package

	/**
	 * 
	 */
	class UMLClassHeaderFigure extends NodeFigure {

		/**
		 * 
		 */
		public UMLClassHeaderFigure() {
			ToolbarLayout layout = new ToolbarLayout();
			setLayoutManager(layout);
			layout.setSpacing(2);
			setOpaque(false); // non-transparent figure
			setBorder( new LineBorder(ColorConstants.black));
		}

		/**
		 * setBound is called to resize or move graphical Element by Eclipse
		 * In the case of this package we have to call setDimension Before.
		 * 
		 * @param rect coordinate of the contentPackage.
		 * 
		 * @see setDimension
		 */

		@Override
		public void setBounds(Rectangle rect) {
			int headerHeight = 20;
			int headerWidth  = rect.width;
			Rectangle headerRect = new Rectangle();
			
			// Calculate height
			if (getStereotypesLabel() != null) {
				headerHeight = headerHeight + 20;
			}
			if( getIconLabel()!=null){
				headerHeight = headerHeight + 20;
			}
			if (getQualifiedNameLabel() != null) {
				headerHeight = headerHeight + 20;
			}
			
			headerRect.setSize(headerWidth, headerHeight);
			headerRect.setLocation(0, (this.getParent().getBounds().height-headerHeight)/2);
			super.setBounds(headerRect);
			Debug.debug(this," bounds are : " +getBounds(), debugChannel);
		}
	}


	/**
	 * 
	 */
	public CollaborationUseFigure()
	{
		ToolbarLayout layout = new ToolbarLayout();
		setLayoutManager(layout);
		for (int i=0;i<10;i++) {
			dashes[i]=5;
		}

		headerFigure = new UMLClassHeaderFigure();
		add(headerFigure);
		super.setBorder(null);
		setOpaque(false);
	}

	/**
	 * set the border coler of the figure.
	 * 
	 * @param borderColor 
	 */
	@Override
	public void setBorderColor(Color borderColor) {
		this.borderColor = borderColor;
		getHeader().setBorder(null);
	}
	
	/**
	 * 
	 * 
	 * @param graphics 
	 */
	@Override
	public void paintBorder(Graphics graphics) {
	}

	/**
	 * this method is used to display  dashes is the composite is extern.
	 * 
	 * @param value to set if the the class is extern
	 */
	public void setExtern(boolean value){
		this.extern =value;
		this.repaint();
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.common.figures.AbstractElementFigure#setBackgroundColor(org.eclipse.swt.graphics.Color)
	 */
	/**
	 * 
	 * 
	 * @param backgroundColor 
	 */
	@Override
	public void setBackgroundColor(Color backgroundColor) {
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.common.figures.AbstractElementFigure#setFontColor(org.eclipse.swt.graphics.Color)
	 */
	/**
	 * 
	 * 
	 * @param color 
	 */
	@Override
	public void setFontColor(Color color) {
		this.fontColor=color;
		getHeader().setFontColor(color);
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.common.figures.AbstractElementFigure#setForeGroundColor(org.eclipse.swt.graphics.Color)
	 */
	/**
	 * 
	 * 
	 * @param color 
	 */
	@Override
	public void setForeGroundColor(Color color) {
		super.setForeGroundColor(color);

	}
	
	/**
	 * setBound is called to resize or move graphical Element by Eclipse
	 * It call setBound of each subFigure.
	 * In the case of contentPackage we call setDimension
	 * 
	 * @param rect coordinate of the contentPackage.
	 * 
	 * @see setDimension
	 */

	@Override
	public void setBounds(Rectangle rect) {
		// Header
		headerFigure.setBounds(new Rectangle(rect));
		super.setBounds(new Rectangle(rect));
		Debug.debug(this," bounds are : " +getBounds(), debugChannel);
	}



	/**
	 * to obtain the header of the package figure.
	 * 
	 * @return IFigure the header
	 */
	public UMLClassHeaderFigure getHeader() {
		return headerFigure;
	}


	/**
	 * 
	 * 
	 * @return 
	 */
	@Override
	protected boolean useLocalCoordinates(){return true;}

	/**
	 * 
	 * 
	 * @return 
	 */
	public int getHeadWitdh(){ return headerFigure.getPreferredSize().width;}
	
	/**
	 * 
	 * 
	 * @return 
	 */
	public int getHeadHeight(){ return headerFigure.getPreferredSize().height;}

	// Override method from AbstractElementFigure
	/**
	 * 
	 * 
	 * @return Returns the nameLabel.
	 */
	@Override
	public Label getNameLabel() {
		return headerFigure.getNameLabel();
	}
	
	/**
	 * 
	 * 
	 * @return Returns the qualifiedNameLabel.
	 */
	@Override
	public Label getQualifiedNameLabel() {
		return headerFigure.getQualifiedNameLabel();
	}
	
	/**
	 * 
	 * 
	 * @return Returns the nameLabel.
	 */
	@Override
	public Label getStereotypesLabel() {
		return headerFigure.getStereotypesLabel();
	}

	/**
	 * 
	 * 
	 * @param name 
	 */
	@Override
	public void setName( String name )
	{
		headerFigure.setName(name);
	}
	
	/**
	 * 
	 * 
	 * @param packageName 
	 */
	@Override
	public void setQualifiedName( String packageName )
	{
		headerFigure.setQualifiedName(packageName);
	}
	
	/**
	 * Set the stereotype value.
	 * If param is null, remove the label.
	 * 
	 * @param stereotypes 
	 */
	@Override
	public void setStereotypes( String stereotypes )
	{
		headerFigure.setStereotypes(stereotypes);
	}

	/**
	 * Refresh presentation of current figure
	 * with presentation specified in a GraphNode.
	 * 
	 * @param presentation 
	 * @param selection 
	 */
	@Override
	public void refreshPresentation(String presentation, Element selection, GraphElement ge) {
		headerFigure.refreshPresentation(presentation, selection, ge);
	}

	/**
	 * Refresh list of stereotypes of current EditPart if required
	 * either with or without the first stereotype.
	 * 
	 * @param presentation 
	 * @param hasIcon 
	 * @param hasShape 
	 * @param stereotypes 
	 */
	@Override
	public void refreshStereotypes(String stereotypes, String presentation, boolean hasIcon, boolean hasShape) {
		headerFigure.refreshStereotypes(stereotypes, presentation, hasIcon, hasShape);

	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.draw2d.Figure#paint(org.eclipse.draw2d.Graphics)
	 */
	public void paint (Graphics graphics) {
		super.paint(graphics);
		graphics.setAntialias(SWT.ON);
		Rectangle rect=getBounds().getCopy();
		rect.height=rect.height-1;
		rect.width=rect.width-1;
		graphics.setLineDash(dashes);
		graphics.setForegroundColor(this.getBorderColor());
		graphics.drawOval(rect);
	}
}